﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace CDSysLDManageProject
{
    public partial class FormArgSetting : Form
    {
        string ArgSetPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
        public FormArgSetting()
        {
            InitializeComponent();
            LoadXMLFile(ArgSetPath);
        }


       



        //创建或获取XML的配置文件的地址
        public void LoadXMLFile(string path)
        {



            XmlDocument xmlDoc = new XmlDocument();
            string XMLPath = path;
            XMLPath = XMLPath + "ArugmentSetting.xml";
            //如果不存在导出路径XML的话则创建。
            if (!File.Exists(XMLPath))
            {
                //创建类型声明节点  
                XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "");
                xmlDoc.AppendChild(node);
                //创建根节点  

                XmlNode root = xmlDoc.CreateElement("参数");
                xmlDoc.AppendChild(root);
                string SysPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                CreateNode(xmlDoc, root, "是否自动搬出", "1");
                CreateNode(xmlDoc, root, "允许NG个数", "0");
               

                try
                {
                    XMLPath = path;
                    XMLPath = XMLPath + "\\" + "ArugmentSetting.xml";
                    xmlDoc.Save(XMLPath);
                    return;
                }
                catch (Exception e)
                {
                    //显示错误信息  
                    Console.WriteLine(e.Message);
                }

            }
            else
            {
                XMLPath = path + "\\" + "ArugmentSetting.xml";
                xmlDoc.Load(XMLPath);
                //获取某个节点
                XmlNode AutoOut = xmlDoc.SelectSingleNode("参数").SelectSingleNode("是否自动搬出");
                XmlNode AllowNgNo = xmlDoc.SelectSingleNode("参数").SelectSingleNode("允许NG个数");


                tbxallowngno.Text = AllowNgNo.InnerText;
                if (AutoOut.InnerText == "1")
                {
                    cbautoout.Checked = true;
                }else
                {
                    cbautoout.Checked = false;
                }
               

            }
            return;

        }


        public void CreateNode(XmlDocument xmlDoc, XmlNode parentNode, string name, string value)
        {
            XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, null);
            node.InnerText = value;
            parentNode.AppendChild(node);
        }

     

        
        private void save_Click_1(object sender, EventArgs e)
        {
            XmlDocument xmlDoc = new XmlDocument();
            string XMLPath = ArgSetPath;
            XMLPath = XMLPath + "\\" + "ArugmentSetting.xml";
            //如果不存在导出路径XML的话则创建。
            if (!File.Exists(XMLPath))
            {
                //创建类型声明节点  
                XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "");
                xmlDoc.AppendChild(node);
                //创建根节点  

                XmlNode root = xmlDoc.CreateElement("参数");
                xmlDoc.AppendChild(root);
                string SysPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                CreateNode(xmlDoc, root, "是否自动搬出", "1");


                try
                {
                    XMLPath = ArgSetPath;
                    XMLPath = XMLPath + "\\" + "ArugmentSetting.xml";
                    xmlDoc.Save(XMLPath);
                    return;
                }
                catch (Exception exp)
                {
                    //显示错误信息  
                    Console.WriteLine(exp.Message);
                }

            }
            else
            {
                XMLPath = ArgSetPath + "\\" + "ArugmentSetting.xml";
                xmlDoc.Load(XMLPath);
                //获取某个节点
                XmlNode autoout = xmlDoc.SelectSingleNode("参数").SelectSingleNode("是否自动搬出");

                if (cbautoout.Checked == true)
                {
                    autoout.InnerText = "1";
                }
                else
                {
                    autoout.InnerText = "0";
                }


                try
                {
                    XMLPath = ArgSetPath;
                    XMLPath = XMLPath + "\\" + "ArugmentSetting.xml";
                    xmlDoc.Save(XMLPath);

                }
                catch (Exception exp)
                {
                    //显示错误信息  
                    Console.WriteLine(exp.Message);
                }

            }
            this.Close();
            
        }

        private void FormArgSetting_Load(object sender, EventArgs e)
        {

        }


    
    }
}
